import Vue from 'vue'
import Router from 'vue-router'
import i18n from '@/lang'
Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */

// 菜单它是读取了路由配制生成的
// 特点：hidden:true,不会在菜单中显示
// 多层菜单：childre有多个项，只有一个项还是单层菜单

/*
将路由分成二块
  静态路由：不需要权限访问的路由  首页   login  404
  动态路由：需要权限访问的路由
  默认只有静态路由
  动态路由是在获取用户信息中的权限数据后匹配相应路由再做添加
  (将有权限的路由匹配出来添加)
     在哪里匹配和添加

*/
// 动态路由
export const otherRoutes = [
  {
    path: '/employees',
    component: Layout,
    val: 'employees',
    children: [
      {
        path: '', // /开头绝对地址  非/开头：它是相对地址，相对于父级 /employees
        name: 'Employees',
        component: () => import('@/views/employees/index'),
        meta: { title: i18n.t('route.employees'), icon: 'people' }
      },
      {
        path: 'import', // /开头绝对地址  非/开头：它是相对地址，相对于父级 /employees
        name: 'Import',
        component: () => import('@/views/employees/import'),
        meta: { title: '员工导入', icon: 'people' },
        hidden: true
      },
      {
        path: 'detail/:id', // /开头绝对地址  非/开头：它是相对地址，相对于父级 /employees
        name: 'Detail',
        component: () => import('@/views/employees/detail'),
        hidden: true
      }
    ]
  },
  {
    path: '/setting',
    component: Layout,
    val: 'settings',
    children: [{
      path: '', // /开头绝对地址  非/开头：它是相对地址，相对于父级 /setting
      name: 'Setting',
      component: () => import('@/views/setting/index'),
      meta: { title: i18n.t('route.settings'), icon: 'setting' }
    }
    ]
  },
  {
    path: '/permission',
    component: Layout,
    val: 'permissions',
    children: [{
      path: '', // /开头绝对地址  非/开头：它是相对地址，相对于父级 /setting
      name: 'Permission',
      component: () => import('@/views/permission/index'),
      meta: { title: '权限管理', icon: 'lock' }
    }
    ]
  },
  {
    path: '/social',
    component: Layout,
    val: 'social_securitys',
    children: [{
      path: '', // /开头绝对地址  非/开头：它是相对地址，相对于父级 /setting
      name: 'Social',
      component: () => import('@/views/social/index'),
      meta: { title: '社保', icon: 'table' }
    }
    ]
  },
  {
    path: '/approvals',
    val: 'approvals',
    component: Layout,
    children: [{
      path: '', // /开头绝对地址  非/开头：它是相对地址，相对于父级 /setting
      name: 'Approvals',
      component: () => import('@/views/approvals/index'),
      meta: { title: '审批', icon: 'tree-table' }
    }
    ]
  },
  {
    path: '/attendances',
    val: 'attendances',
    component: Layout,
    children: [{
      path: '', // /开头绝对地址  非/开头：它是相对地址，相对于父级 /setting
      name: 'Attendances',
      component: () => import('@/views/attendances/index'),
      meta: { title: '考勤', icon: 'skill' }
    }
    ]
  },
  {
    path: '/salarys',
    component: Layout,
    val: 'salarys',
    children: [{
      path: '', // /开头绝对地址  非/开头：它是相对地址，相对于父级 /setting
      name: 'Salarys',
      component: () => import('@/views/salarys/index'),
      meta: { title: '工资', icon: 'money' }
    }
    ]
  },
  {
    path: '/departments',
    component: Layout,
    val: 'departments',
    children: [{
      path: '', // /开头绝对地址  非/开头：它是相对地址，相对于父级 /setting
      name: 'Departments',
      component: () => import('@/views/departments/index'),
      meta: { title: '组织架构', icon: 'tree' }
    }
    ]
  }
]

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: i18n.t('route.dashboard'), icon: 'dashboard' }
      // meta: { title: 'menu.dashboard', icon: 'dashboard' }
    }
    ]
  }
  // ...otherRoutes,
  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // 重置路由
}

export default router
